import time import RPi.GPIO as GPIO import os import socket # Configuration réseau et InfluxDB hostname = socket.gethostname() cityserver = "172.16.13.7" db_name = "proximity_sensor" record_name = "proximity_measure" username = "influx_user" password = "w1GYhmBvo" # Configuration des broches GPIO GPIO.setmode(GPIO.BCM) GPIO_TRIGGER = 17 GPIO_ECHO = 27 GPIO.setup(GPIO_TRIGGER, GPIO.OUT) GPIO.setup(GPIO_ECHO, GPIO.IN) # Stabilisation du capteur GPIO.output(GPIO_TRIGGER, False) time.sleep(0.5) try: while True: # Envoi d'une impulsion de 10µs GPIO.output(GPIO_TRIGGER, True) time.sleep(0.00001) GPIO.output(GPIO_TRIGGER, False) # Attente du début de la réception while GPIO.input(GPIO_ECHO) == 0: start = time.time() # Attente de la fin de la réception while GPIO.input(GPIO_ECHO) == 1: stop = time.time() # Calcul de la distance elapsed = stop - start distance = (elapsed * 34300) / 2 # Vitesse du son ~343 m/s dis_rounded = round(distance, 2) dis_text = str(dis_rounded) # Envoi des données à InfluxDB via curl cmd = ( "curl -i -XPOST 'http://{server}:8086/write?" "u={user}&p={pwd}&db={db}' --data-binary " "'{rec},host={host} value={val}'" ).format( server=cityserver, user=username, pwd=password, db=db_name, rec=record_name, host=hostname, val=dis_text ) os.system(cmd) time.sleep(1) except KeyboardInterrupt: print("Mesure interrompue par l'utilisateur") finally: GPIO.cleanup()